package com.amesit.bean.privilege;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

@Entity
public class PrivilegeGroup implements Serializable {
	private Integer id;
	private String name;
	private Set<SystemPrivilege> privileges = new HashSet<SystemPrivilege>();
	
	public PrivilegeGroup(){};
	
	public PrivilegeGroup(Integer id) {
		this.id = id;
	}
	
	public PrivilegeGroup(String name) {
		this.name = name;
	}
	
	@Id @GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@Column(length=30,nullable=false)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@ManyToMany(cascade=CascadeType.REFRESH, fetch=FetchType.EAGER)
	@JoinTable(name="group_privilege",joinColumns=@JoinColumn(name="group_id"),
			inverseJoinColumns={@JoinColumn(name="model",referencedColumnName="model"),
								@JoinColumn(name="privilegeValue",referencedColumnName="privilegeValue")})
	public Set<SystemPrivilege> getPrivileges() {
		return privileges;
	}
	public void setPrivileges(Set<SystemPrivilege> privileges) {
		this.privileges = privileges;
	}
	
	public void addPrivilege(SystemPrivilege privilege){
		if(!this.privileges.contains(privilege)) this.privileges.add(privilege);
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final PrivilegeGroup other = (PrivilegeGroup) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}
